package com.ehi.alliance.resource;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import com.ehi.alliance.service.AllianceService;
import com.ehi.common.domain.alliance.AllianceInfo;
import com.google.common.collect.Lists;
import io.reactivex.Observable;
import org.glassfish.jersey.server.ManagedAsync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Component
@Path("/echo")
public class Echo {

    @Autowired
    private AllianceService allianceService;

    @Value("${achui.test1:default1}")
    private String value1;

    @Value("${achui.test2:default2}")
    private String value2;

    @Value("${alliance.port:default}")
    private String value;

    @Autowired
    DiscoveryClient discoveryClient;


    @GET
    public String echo() {
        System.out.println("client:" + discoveryClient);
        Config config = ConfigService.getAppConfig();
        String value3 = config.getProperty("timeout", "default-port");
        return "Hello Spring Boot29" + " value1:" + value1 + " value2:" + value2 + " value:" + value + " ctrip:" + value3;
    }


    @GET
    @Path("/city")
    @Produces(MediaType.APPLICATION_JSON)
    public AllianceInfo getAllianceInfo() {
        return allianceService.getByAllianceId("5");
    }

    @GET
    @Path("/city2")
    @ManagedAsync
    @Produces(MediaType.APPLICATION_JSON)
    public void getAllianceInfo2(@Suspended final AsyncResponse asyncResponse) {
        Observable<AllianceInfo> observable1 = Observable.create(emmiter -> {
            AllianceInfo allianceInfo = allianceService.getByAllianceId("1");
            if(allianceInfo != null) {
                emmiter.onNext(allianceInfo);
            }
            emmiter.onComplete();
        });
        Observable<AllianceInfo> observable2 = Observable.create(emmiter -> {
            AllianceInfo allianceInfo = allianceService.getByAllianceId("2");
            if(allianceInfo != null) {
                emmiter.onNext(allianceInfo);
            }
            emmiter.onComplete();
        });
        Observable<AllianceInfo> observable3 = Observable.create(emmiter -> {
            AllianceInfo allianceInfo = allianceService.getByAllianceId("3");
            if(allianceInfo != null) {
                emmiter.onNext(allianceInfo);
            }
            emmiter.onComplete();
        });
        Observable<AllianceInfo> observable4 = Observable.create(emmiter -> {
            AllianceInfo allianceInfo = allianceService.getByAllianceId("4");
            if(allianceInfo != null) {
                emmiter.onNext(allianceInfo);
            }
            emmiter.onComplete();
        });
        List<AllianceInfo> list = Lists.newArrayList();
        Observable.zip(observable1, observable2, observable3, observable4, (o1, o2, o3, o4) -> {
            list.add(o1);
            list.add(o2);
            list.add(o3);
            list.add(o4);
            System.out.println("o4:"+ o4.getName());
            return list;
        }).subscribe(lists -> {
            System.out.println("----size:" + lists.size());
            asyncResponse.resume(lists);
        });

        //return allianceService.getByAllianceId("4");
    }
}
